package org.apache.commons.graph.algorithms.spanning

import org.apache.commons.graph.Graph
import org.apache.commons.graph.GraphException
import org.apache.commons.graph.VertexPair

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
 * @param <V>
 * @param <WE>
</WE></V> */
internal class ReverseDeleteGraph<V, WE>(
    private val graph: Graph<V, WE>,
    private val sortedEdge: Collection<WE>,
    private val visitedEdge: Collection<WE>
) : Graph<V, WE> {

    override fun containsEdge(e: WE): Boolean {
        return graph.containsEdge(e)
    }

    override fun containsVertex(v: V): Boolean {
        return graph.containsVertex(v)
    }

    override fun getConnectedVertices(v: V): Iterable<V> {
        val tmp: MutableList<V> = ArrayList()
        for (originalVertex in graph.getConnectedVertices(v)) {
            if (getEdge(v, originalVertex) != null) {
                tmp.add(originalVertex)
            }
        }

        return tmp
    }

    override fun getDegree(v: V): Int {
        throw GraphException("Unused Method")
    }

    override fun getEdge(source: V, target: V): WE? {
        val edge = graph.getEdge(source, target)
        if (sortedEdge.contains(edge)) {
            return edge
        }

        if (visitedEdge.contains(edge)) {
            return edge
        }
        return null
    }

    override val edges: Iterable<WE>
        get() {
            val e: MutableList<WE> = ArrayList()
            e.addAll(sortedEdge)
            e.addAll(visitedEdge)
            return e
        }

    override val order: Int
        get() = graph.order

    override val size: Int
        get() = sortedEdge.size + visitedEdge.size

    override val vertices: Iterable<V>
        get() = graph.vertices

    override fun getVertices(e: WE): VertexPair<V>? {
        for (edge in sortedEdge) {
            if (edge == e) {
                return graph.getVertices(e)
            }
        }

        if (sortedEdge.contains(e)) {
            return graph.getVertices(e)
        }

        if (visitedEdge.contains(e)) {
            return graph.getVertices(e)
        }
        return null
    }

    companion object {
        private const val serialVersionUID = -543197749473412325L
    }
}

fun<V, WE> Graph<V, WE>.reverseDeleteGraph(sortedEdge: Collection<WE>,
                                           visitedEdge: Collection<WE>):Graph<V, WE> = ReverseDeleteGraph(this,sortedEdge, visitedEdge)